import 'package:flutter/material.dart';
import 'dart:async';

class SelectableTypewriterText extends StatefulWidget {
  final SelectableText textWidget;
  final Duration duration;
  final bool needType;
  final Function onComplete;
  final Function onLineBreak;

  const SelectableTypewriterText({
    Key? key,
    required this.textWidget,
    this.needType = true,
    required this.onComplete,
    required this.onLineBreak,
    this.duration = const Duration(milliseconds: 100),
  }) : super(key: key);

  @override
  _SelectableTypewriterTextState createState() => _SelectableTypewriterTextState();
}

class _SelectableTypewriterTextState extends State<SelectableTypewriterText> {
  String _displayText = '';
  int _currentIndex = 0;
  Timer? _timer;

  @override
  void initState() {
    super.initState();
    if(widget.needType){
      _startTypewriter();
    }else{
      setState(() {
        _displayText = widget.textWidget.data!;
      });
    }
  }

  void _startTypewriter() {
    _timer = Timer.periodic(widget.duration, (timer) {
      if (_currentIndex < widget.textWidget.data!.length) {
        setState(() {
          _displayText += widget.textWidget.data![_currentIndex];
          _currentIndex++;
          _checkLineBreak(); // 检查是否换行
        });
      } else {
        widget.onComplete();
        timer.cancel();
      }
    });
  }


  void _checkLineBreak() {
    final textPainter = TextPainter(
      text: TextSpan(text: _displayText, style: widget.textWidget.style),
      textDirection: TextDirection.ltr,
    );
    textPainter.layout(maxWidth: MediaQuery.of(context).size.width);

    final textHeight = textPainter.size.height;
    if (textHeight > widget.textWidget.style!.fontSize!) {
      widget.onLineBreak(); // 触发换行回调
    }
  }

  @override
  void dispose() {
    _timer?.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return SelectableText(
      _displayText,
      style: widget.textWidget.style,
      textAlign: widget.textWidget.textAlign,
      maxLines: widget.textWidget.maxLines,
      textScaleFactor: widget.textWidget.textScaleFactor,
      strutStyle: widget.textWidget.strutStyle,
      textDirection: widget.textWidget.textDirection,
      textWidthBasis: widget.textWidget.textWidthBasis,
      textHeightBehavior: widget.textWidget.textHeightBehavior,
      contextMenuBuilder: widget.textWidget.contextMenuBuilder,
    );
  }
}




class TypewriterText extends StatefulWidget {
  final String text;
  final Duration duration;
  final TextStyle style;

  const TypewriterText(this.text,{
    Key? key,
    this.duration = const Duration(milliseconds: 100),
    this.style = const TextStyle(),
  }) : super(key: key);

  @override
  _TypewriterTextState createState() => _TypewriterTextState();
}

class _TypewriterTextState extends State<TypewriterText> {
  String _displayText = '';
  int _currentIndex = 0;
  Timer? _timer;

  @override
  void initState() {
    super.initState();
    _startTypewriter();
  }

  void _startTypewriter() {
    _timer = Timer.periodic(widget.duration, (timer) {
      if (_currentIndex < widget.text.length) {
        setState(() {
          _displayText += widget.text[_currentIndex];
          _currentIndex++;
          _checkLineBreak(); // 检查是否换行
        });
      } else {
        timer.cancel();
      }
    });
  }


  void _checkLineBreak() {
    final textPainter = TextPainter(
      text: TextSpan(text: _displayText, style: widget.style),
      textDirection: TextDirection.ltr,
    );
    textPainter.layout(maxWidth: MediaQuery.of(context).size.width);
  }

  @override
  void dispose() {
    _timer?.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // return SelectableText(
    //   _displayText,
    //   style: widget.textWidget.style,
    //   textAlign: widget.textWidget.textAlign,
    //   maxLines: widget.textWidget.maxLines,
    //   textScaleFactor: widget.textWidget.textScaleFactor,
    //   strutStyle: widget.textWidget.strutStyle,
    //   textDirection: widget.textWidget.textDirection,
    //   textWidthBasis: widget.textWidget.textWidthBasis,
    //   textHeightBehavior: widget.textWidget.textHeightBehavior,
    //   contextMenuBuilder: widget.textWidget.contextMenuBuilder,
    // );
    return Text(_displayText , style: widget.style,);
  }
}
